package HuaRongDao;

import java.io.*;

public class MapFactory {

    //ID编号对应关系：0曹操，1赵云，2张飞，3关羽，4马超，5黄忠，6小兵，7小兵，8小兵，9小兵
    //Node(id,x,y,direction)，id对应每个角色,x,y对应左上角数组下标，direction为true时，横放，false时，竖放
    static String[] array;
    public static Node[][] map = new Node[][]{

            {//七步成诗
                    new Node(0,2,2,true),new Node(1,2,0,false),new Node(2,0,1,false),
                    new Node(3,0,3,false),new Node(4,0,0,false),new Node(5,2,1,false),
                    new Node(6,4,0,true),new Node(7,4,1,true),new Node(8,4,2,true),new Node(9,4,3,true)
            },

            {//横刀立马
                    new Node(0,0,1,true),new Node(1,0,3,false),new Node(2,2,3,false),
                    new Node(3,2,1,true),new Node(4,0,0,false),new Node(5,2,0,false),
                    new Node(6,4,0,true),new Node(7,3,1,true),new Node(8,3,2,true),new Node(9,4,3,true)
            },

            {//屯兵东路
                    new Node(0,0,0,true),new Node(1,0,3,false),new Node(2,0,2,false),
                    new Node(3,2,0,true),new Node(4,3,0,false),new Node(5,3,1,false),
                    new Node(6,2,2,true),new Node(7,2,3,true),new Node(8,3,2,true),new Node(9,3,3,true)
            },
            {//插翅难飞
                    new Node(0,0,1,true),new Node(1,0,0,false),new Node(2,4,2,true),
                    new Node(3,4,0,true),new Node(4,2,1,false),new Node(5,0,3,false),
                    new Node(6,2,0,true),new Node(7,3,0,true),new Node(8,2,3,true),new Node(9,3,3,true)
            },
            {//巧过五关
                    new Node(0,0,1,true),new Node(1,2,2,true),new Node(2,3,2,true),
                    new Node(3,4,1,true),new Node(4,3,0,true),new Node(5,2,0,true),
                    new Node(6,0,0,true),new Node(7,1,0,true),new Node(8,0,3,true),new Node(9,1,3,true)
            },
            {//层层设防
                    new Node(0,0,1,true),new Node(1,1,3,false),new Node(2,1,0,false),
                    new Node(3,4,1,true),new Node(4,2,1,true),new Node(5,3,1,true),
                    new Node(6,0,0,true),new Node(7,3,0,true),new Node(8,0,3,true),new Node(9,3,3,true)
            },
            {//近在咫尺
                    new Node(0,3,2,true),new Node(1,0,1,false),new Node(2,3,0,true),
                    new Node(3,2,0,true),new Node(4,0,3,false),new Node(5,0,2,false),
                    new Node(6,0,0,true),new Node(7,1,0,true),new Node(8,2,2,true),new Node(9,2,3,true)
            },
            {//兵临曹营
                    new Node(0,0,1,true),new Node(1,3,1,false),new Node(2,3,2,false),
                    new Node(3,2,1,true),new Node(4,2,0,false),new Node(5,2,3,false),
                    new Node(6,0,0,true),new Node(7,1,0,true),new Node(8,0,3,true),new Node(9,1,3,true)
            },
            {//众志成城
                    new Node(0,1,1,true),new Node(1,3,1,false),new Node(2,3,3,false),
                    new Node(3,0,2,true),new Node(4,0,0,false),new Node(5,2,0,false),
                    new Node(6,0,1,true),new Node(7,1,3,true),new Node(8,2,3,true),new Node(9,4,2,true)
            },
            {//佳人梳妆
                    new Node(0,1,0,true),new Node(1,3,1,false),new Node(2,3,2,false),
                    new Node(3,0,2,true),new Node(4,1,2,false),new Node(5,2,3,false),
                    new Node(6,3,0,true),new Node(7,4,0,true),new Node(8,1,3,true),new Node(9,4,3,true)
            }
    };



    //返回指定关卡布局信息的拷贝（避免直接引用）
    public static Node[] getMap(int level){

        Node[] temp = new Node[10];
        for(int i=0;i<10;i++){
            temp[i] = map[level-1][i];
        }

        return temp;
    }

    public static int getRecord(int level){

        FileOutputStream fos = null;
        DataOutputStream dos = null;
        FileInputStream fis = null;
        DataInputStream dis = null;

        File file = new File("src/main/java/HuaRongDao/GameRecordAboutSwing");

        if(!file.exists()){
            file.mkdirs();
        }
        File record = new File("src/main/java/HuaRongDao/GameRecordAboutSwing/recordKlotskiGame.txt");

        try{
            if(!record.exists()){//如果不存在,新建文本
                record.createNewFile();
                fos = new FileOutputStream(record);
                dos = new DataOutputStream(fos);
                String s = "9999,9999,9999,9999,9999,9999,9999,9999,9999,9999";
                dos.writeBytes(s);
                System.out.println(record.isFile());;
            }
            //读取记录
            fis = new FileInputStream(record);
            dis = new DataInputStream(fis);
            String str = dis.readLine();
            array = str.split(",");

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(fis!=null)
                    fis.close();
                if(dis!=null)
                    dis.close();
                if(fos!=null)
                    fos.close();
                if(dos!=null)
                    dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return Integer.parseInt(array[level-1]);

    }

    public static void writeRecord(int level,int step){

        FileOutputStream fos = null;
        DataOutputStream dos = null;
        File record = new File("src/main/java/HuaRongDao/GameRecordAboutSwing/recordKlotskiGame.txt");

        try {
            //清空原有记录
            FileWriter fileWriter =new FileWriter(record);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
            //重新写入文本
            fos = new FileOutputStream(record);
            dos = new DataOutputStream(fos);
            array[level-1] = step+"";
            StringBuilder s = new StringBuilder();
            s.append(array[0]);
            for(int i=1;i<array.length;i++){
                s.append(","+array[i]);
            }System.out.println(s.toString());
            dos.writeBytes(s.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(fos!=null)
                    fos.close();
                if(dos!=null)
                    dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }



    }


    public static void hello(){

    }


}
